//
//  NSMutableString+THHook.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/14.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSMutableString+THHook.h"
#import "THDeBugTool.h"
#import "THCrashManager.h"
@implementation NSMutableString (THHook)

+ (void)load{
#ifdef DEBUG
    return;
#endif
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cla  = NSClassFromString(@"__NSCFString");
               SEL selectors[] = {
                   @selector(replaceCharactersInRange:withString:),
                   @selector(insertString:atIndex:),
                   @selector(deleteCharactersInRange:),
               };
               SEL hookselectors[] = {
                   @selector(zh_HookreplaceCharactersInRange:withString:),
                   @selector(zh_HookinsertString:atIndex:),
                   @selector(zh_HookdeleteCharactersInRange:),
               };
               for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
                   SEL originalSelector = selectors[index];
                   SEL swizzledSelector = hookselectors[index];
                   BOOL hook = ExchangeImplementationsInTwoClasses(cla,originalSelector,[NSMutableString class], swizzledSelector);
                   if (!hook) {
                       NSLog(@"拦截失败");
                   }
               }
    });
}
- (void)zh_HookreplaceCharactersInRange:(NSRange)range withString:(NSString *)aString {
    @try {
        [self zh_HookreplaceCharactersInRange:range withString:aString];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
    }
}
#pragma mark - insertString:atIndex:
- (void)zh_HookinsertString:(NSString *)aString atIndex:(NSUInteger)loc {
    @try {
        [self zh_HookinsertString:aString atIndex:loc];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
    }
}
#pragma mark - deleteCharactersInRange
- (void)zh_HookdeleteCharactersInRange:(NSRange)range {
    @try {
        [self zh_HookdeleteCharactersInRange:range];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
        
    }
}

@end
